/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.db.rawsql;

import org.sng.util.db.DbConn;
import org.sng.util.db.rawsql.value.*;
import org.sng.util.slog.SLog;

public class SQLInsert extends RawSQL {
	
	protected Value m_values;
	
	protected SQLInsert() {
		super();
		
		this.m_values = null;
	}
	
	protected SQLInsert(String s) {
		super(s);
		
		this.m_values = null;
	}
	
	public DbConn Execute()
	{
		this.m_dbo.executeNonQuery(this.ToSQL());
		return null;
	}
	
	public DbConn Execute(boolean tag)
	{
		String sql = this.ToSQL();
		
		SLog.println(sql);
		
		this.m_dbo.executeNonQuery(sql);
		return null;
	}
	
	/* ------ Output ------ */
	public String ToSQL()
	{
		StringBuffer sb = new StringBuffer();
		
		sb.append("insert into ");
		
		sb.append(this.m_target.AllToString());
		
		sb.append(" ");
		
		if (this.m_values != null)
		{
			sb.append(this.m_values.AllToInsertString());
		}
		
		return sb.toString();
	}
	
	/* ------ Values ------ */
	public SQLInsert Values(String header, int n)
	{
		if (this.m_values == null)
		{
			this.m_values = new ValueForInt(header, n);
		}
		else
		{
			this.m_values.AppendValue(new ValueForInt(header, n));
		}
		
		return this;
	}
	
	public SQLInsert Values(String header, double d)
	{
		if (this.m_values == null)
		{
			this.m_values = new ValueForDouble(header, d);
		}
		else
		{
			this.m_values.AppendValue(new ValueForDouble(header, d));
		}
		
		return this;
	}
	
	public SQLInsert Values(String header, String s)
	{
		if (this.m_values == null)
		{
			this.m_values = new ValueForString(header, s);
		}
		else
		{
			this.m_values.AppendValue(new ValueForString(header, s));
		}
		
		return this;
	}

}
